<template>
	<view class="mainpadding2" style="padding-bottom: 150rpx;">
		<view class="mainpadding_top ffffff radius">
			<view class="titletext xiaohei fonweight">标题</view>
			<textarea class="textarea margin_top" placeholder="请输入标题内容" v-model="title"></textarea>
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="titletext xiaohei fonweight">描述</view>
			<textarea class="textarea margin_top" placeholder="请输入描述内容" v-model="content"></textarea>
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="titletext xiaohei fonweight">上传图片（{{images_text.length}}/9）</view>
			<view class="flexleft flex_wrap">
				<view class="sanshis flexcenter" v-for="(item,index) in images_text" :key="index">
					<view class="dingwei margin_top" style="width: 180rpx;height: 180rpx;">
						<image :src="item" class=" radius" style="width: 100%;height: 100%;" mode=""></image>
						<view class="deleteimg" @click="delindex(index,images,images_text)"></view>
					</view>
				</view>
				<view class="sanshis  flexcenter" @click="uploadImg(9,images,images_text)" v-if="images_text.length<9">
					<image src="@/static/images/tjtp.png" class="margin_top" style="width: 180rpx;height: 180rpx;"
						mode=""></image>
				</view>
			</view>
		</view>
		<view class="gudingdb mainpadding">
			<view class="bigbtn" @click="submit()">提交</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")

	export default {
		data() {
			return {
				title: "",
				content: "",
				images: [],
				images_text: [],
				listid:"",
				upflag:false,
			}
		},
		onLoad(options) {
			if(options.id){
				uni.setNavigationBarTitle({
					title:"编辑晒票"
				})
				this.listid = options.id
				this.getDetail()
			}
		},
		onShow() {
			this.upflag = false
		},
		methods: {
			getDetail() {
				httpRequest.request('/api/userdtcl/dtDetail', 'GET', {
					id: this.listid
				}, false, false, true).then(res => {
					this.title = res.data.title
					this.content = res.data.content
					this.images = res.data.images.split(',')
					this.images_text = res.data.images_text
				})
			},
			submit(){
				if(this.title==""){
					httpRequest.toast("请输入标题内容")
					return false
				}
				// if(this.content==""){
				// 	httpRequest.toast("请输入描述内容")
				// 	return false
				// }
				if(this.images.length==0){
					httpRequest.toast("请上传图片")
					return false
				}
				let url = "/api/Userdtcl/addUserDt"
				let data = {
					title: this.title,
					content: this.content,
					images: this.images.join(),
				}
				if(this.listid){
					url = "/api/Userdtcl/editUserDt"
					this.$set(data,"id",this.listid)
				}
				if(this.upflag==true) {
					return false
				}
				this.upflag = true
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				httpRequest.request(url, 'GET', data).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}else{
						this.upflag = false
					}
				
				})
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style>
	.textarea {
		height: 150rpx;
		width: 100%;
	}
</style>